<?php

App::uses('AppController', 'Controller');
App::import('Model', 'Champion');

/**
 * Videos Controller
 *
 * @property Video $Video
 */
class VideosController extends AppController {
    
    public function beforeFilter() {
        parent::beforeFilter();
        $this->Auth->allow('add', 'edit', 'delete');
    }

    /**
     * index method
     *
     * @return void
     */
    public function index() {
        $this->Video->recursive = 0;
        $this->set('videos', $this->paginate());
    }

    /**
     * view method
     *
     * @param string $id
     * @return void
     */
    public function view($id = null) {
        if($id==null)
        {
            $this->redirect(array('action' => 'index'));
            return;
        }
        $champion=new Champion();
        $chp=$champion->findById($id);
        $chp=$chp['Champion']['id_video'];
        $aux=array();
        $videos=$this->Video->find('all');
        $vd=$this->Video->find('list', array('conditions' => array('Video.id_champion' => $id)));
        if($vd==null)
            $vd=array('-1'=>'');
        foreach($videos as $videos2){
            foreach($vd as $key => $vd2){
                if($key==$videos2['Video']['id'])
                    $aux+=array($key => $videos2);
                else if($chp==$videos2['Video']['id'])
                    $aux+=array($chp => $videos2);
            }
        }
        $this->set('aux',$aux);
        /*
        $this->Video->id = $id;
        if (!$this->Video->exists()) {
            throw new NotFoundException(__('Invalid video'));
        }
        $this->set('video', $this->Video->read(null, $id));*/
    }

    /**
     * add method
     *
     * @return void
     */
    public function add($data = null) {
            if($data!=null){
                $this->request->data=$data;
                if (empty($this->request->data['Video']['url'])) {
                    $this->Session->setFlash(__('É preciso enviar algum video', true));
                    return false;
                }
                $this->Video->create();
                if ($this->Video->save($this->request->data)) {
                    $this->Session->setFlash(__('The video has been saved'));
                } else {
                    $this->Session->setFlash(__('The video could not be saved. Please, try again.'));
                    return false;
                }
                return $this->Video->find('first', array('order' => array('Video.id DESC')));
            }
            else{
                $champion = new Champion();
                $champions = array('');
                $champions+=$champion->find('list', array('fields' => array('Champion.id', 'Champion.champion_name')));
                $options = array('options' => $champions);
                $this->set('options',$options);
                if($this->request->is('post')){
                    if($this->request->data['Video']['champion']=='0')
                        $this->request->data['Video']['champion']='';
                    else
                        $this->request->data['Video']['id_champion']=$this->request->data['Video']['champion'];
                    if (empty($this->request->data['Video']['url'])||empty($this->request->data['Video']['title'])) {
                        $this->Session->setFlash('Por favor, preencha corretamente os campos de cadastro e tente novamente.','default',array('class' => 'errorMsg'));
                        $this->Video->save($this->request->data);
                        return;
                    }
                    $this->request->data['Video']['user_id']=$this->Auth->user('id');
                    $this->Video->create();
                    if ($this->Video->save($this->request->data)) {
                        $this->Session->setFlash('Game Play cadastrado com sucesso','default',array('class' => 'sucessMsg'));
                        $this->redirect(array('action' => 'index'));
                    } else {
                        $this->Session->setFlash('Por favor, preencha corretamente os campos de cadastro e tente novamente.','default',array('class' => 'errorMsg'));
                    }
                }
            }
    }

    /**
     * edit method
     *
     * @param string $id
     * @return void
     */
    public function edit($id = null) {
        $this->Video->id = $id;
        if (!$this->Video->exists()) {
            throw new NotFoundException(__('Invalid video'));
        }
        if ($this->request->is('post') || $this->request->is('put')) {
            if ($this->Video->save($this->request->data)) {
                $this->Session->setFlash('Game Play cadastrado com sucesso','default',array('class' => 'sucessMsg'));
                $this->redirect(array('action' => 'index'));
            } else {
                $this->Session->setFlash('Por favor, preencha corretamente os campos de cadastro e tente novamente.','default',array('class' => 'errorMsg'));
            }
        } else {
            $this->request->data = $this->Video->read(null, $id);
        }
    }

    /**
     * delete method
     *
     * @param string $id
     * @return void
     */
    public function delete($id = null) {
        if (!$this->request->is('post')) {
            throw new MethodNotAllowedException();
        }
        $this->Video->id = $id;
        if (!$this->Video->exists()) {
            throw new NotFoundException(__('Invalid video'));
        }
        if ($this->Video->delete()) {
            $this->Session->setFlash('Game Play excluído com sucesso','default',array('class' => 'sucessMsg'));
            $this->redirect(array('action' => 'index'));
        }
        $this->Session->setFlash(__('Game Play was not deleted'));
        $this->redirect(array('action' => 'index'));
    }

}
